﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using Ah.Testudin.Engine.Actors.Components;
using Ah.Testudin.Engine.Content;
using Ah.Testudin.Engine.Actors.Components.Render;
using Ah.Testudin.Engine.Graphics.Scenes;
using Ah.Testudin.Engine.Events;
using Ah.Testudin.Engine.Physics;

namespace Ah.Testudin.Engine.Actors.Spawning
{
    public class CubeActorBuilder : IActorBuilder
    {
        #region Private Member Variables

        private const float GRAVITY_ACCELERATION = 1.32f;

        private readonly ITestudinContentManager _contentManager;

        private readonly IEventManager _eventManager;

        private readonly IPhysicsSimulator _physicsSimulator;

        #endregion

        #region Constructors

        public CubeActorBuilder(
            ITestudinContentManager contentManager,
            IEventManager eventManager,
            IPhysicsSimulator physicsSimulator)
        {
            _contentManager = contentManager;

            _eventManager = eventManager;

            _physicsSimulator = physicsSimulator;
        }

        #endregion

        #region IActorBuilder

        public Actor BuildActor(Matrix transform)
        {
            ActorId cubeActorId = ActorIdGenerator.GetNextActorId();

            Actor cubeActor = new Actor(cubeActorId);

            cubeActor.SetComponent<TransformComponent>(
                new TransformComponent(
                    cubeActor,
                    transform));

            Model cubeModel = _contentManager.Load<Model>("Content/ig_box");

            cubeActor.SetComponent<IRenderComponent>(
                new ModelRenderComponent(
                    cubeActor,
                    cubeModel,
                    RenderPass.DynamicActor,
                    _eventManager));

            cubeActor.SetComponent<PhysicsComponent>(
                new PhysicsComponent(
                    cubeActor,
                    Shape.Cube,
                    transform,
                    new Vector3(
                        .5f,
                        .5f,
                        .5f),
                    GRAVITY_ACCELERATION,
                    _physicsSimulator,
                    _eventManager));

            return cubeActor;
        }

        #endregion
    }
}
